<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;

class Index extends Base
{
	public function _initialize()
	{
		$this->checkLogin();
		$this->admin = session('admin');
		$this->assign("title","后台管理");
		//$this->assign("admin",$this->admin);
		$this->assign("adminName",$this->admin['admin_name']);
	}
	
	public function index()
	{
		return $this->fetch();
	}
	
	// 管理员列表
	public function listAdmin() 
	{
		$this->assign("title","后台管理-管理员列表");
		//$adminList = Db::name('sys_admin')->select();
		//$adminList = Db::table('sys_admin')->page(1,1)->select();
		$adminList = Db::name('sys_admin')->cache(true,60)->paginate(20);
		// 获取分页显示
		$page = $adminList->render();
		$this->assign("listAdmin",$adminList);
		$this->assign('page', $page);
		return $this->fetch();
	}
	
	// 添加管理员
	public function addAdmin()
	{ 
		if($this->request->isPost())
		{
			if (empty(input('adminName')) )
			{
				showMsg("用户名不能为空", "");
			}
			
			if (empty(input('adminPwd')) )
			{
				showMsg("密码不能为空", "");
			}
			
			if (empty(input('rePwd')) )
			{
				showMsg("确认密码不能为空", "");
			}
			
			$adminName = input('adminName');
			$pwd = input('adminPwd');
			$rePwd = input('rePwd');
			if($pwd != $rePwd)
			{
				showMsg("两次输入的密码不一致!", "addAdmin");
			}
			
			$arr = array(
					'admin_name' => input('adminName'),
					'admin_pwd' => md5($pwd),
					'email' => $_POST['adminEmail']
			);
			if(Db::name('sys_admin')->insert($arr))
			{
				showMsg("添加管理员{$adminName}成功!继续添加。。。", "");
			}
			else
			{
				showMsg("添加管理员失败!","");
			}
			 
		}
		
		$this->assign("title","后台管理-添加管理员");
		return $this->fetch();
		
	}
	
	// 编辑管理员
	public function editAdmin()
	{
		
		if(!$this->request->post())
		{
			$admin_id = input("id");
			$admin = Db::table('sys_admin')->where('id', $admin_id)->find();
			$this->assign("admin",$admin);
			return $this->fetch('editAdmin');

		}
		else
		{
			$id = input('adminId');
			$pwd = input('adminPwd');
			$rePwd = input('rePwd');
			if($pwd != $rePwd)
			{
				showMsg("两次输入的密码不一致!", url("editAdmin",['id'=>$id]));
			}

			$arr = [
					'admin_name' => input('adminName'),
                    'admin_pwd' => $pwd,
                    'email' => input('adminEmail'),
                    'mobile' => input('adminMobile'),
				   ];
			
			if(Db::table('sys_admin')->where('id', $id)->update($arr))
			{
				showMsg("编辑管理员成功!", url("editAdmin",['id'=>$id]));
			}
			else
			{
				showMsg("编辑管理员失败!", url("listAdmin",['id'=>$id]));
			}

		}
	}
	// 删除管理员
	public function delAdmin()
	{
		 $admin_id = input('id');
		 
		 if(Db::name('sys_admin')->delete($admin_id))
		 {
		 	showMsg("删除成功!", url("listAdmin"));
		 }
		 else 
		 {
		 	showMsg("删除失败!", url("listAdmin"));
		 }
		 		
	}

	// 大管家列表
	public function listButler() 
	{
		$this->assign("title","后台管理-大管家列表");
		$butlerList = Db::name('os_butler')->cache(true,60)->paginate(20);
		// 获取分页显示
		$page = $butlerList->render();
		$this->assign("listButler",$butlerList);
		$this->assign('page', $page);
		return $this->fetch();
	}
	
	// 添加大管家
	public function addButler()
	{ 
		if($this->request->isPost())
		{
			if (empty(input('butlerName')) )
			{
				showMsg("用户名不能为空", "");
			}
			
			if (empty(input('butlerPwd')) )
			{
				showMsg("密码不能为空", "");
			}
			
			if (empty(input('rePwd')) )
			{
				showMsg("确认密码不能为空", "");
			}
			
			$butlerName = input('butlerName');
			$pwd = input('butlerPwd');
			$rePwd = input('rePwd');
			if($pwd != $rePwd)
			{
				showMsg("两次输入的密码不一致!", "addbutler");
			}
			
			if($this->request->file('headImg'))
			{
				$file = $this->request->file('headImg');
				// 移动到框架应用根目录/public/uploads/ 目录下
				$info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
				if($info){
					// 成功上传后 获取上传信息
					// 输出 jpg
					echo $info->getExtension();
					// 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
					echo $info->getSaveName();
					// 输出 42a79759f284b767dfcb2a0197904287.jpg
					echo $info->getFilename();
				}else{
					// 上传失败获取错误信息
					echo $file->getError();
				}
			}
				
			$arr = array(
					'username' => input('butlerName'),
					'password' => md5($pwd),
					'head_img' => input('headImg'),
					'company' => input('company'),
					'position' => input('position'),
			);
			
			if(Db::name('os_butler')->insert($arr))
			{
				showMsg("添加大管家{$butlerName}成功!继续添加。。。",url("addButler"));
			}
			else
			{
				showMsg("添加大管家失败!",url("addButler"));
			}
			 
		}
		
		$this->assign("title","后台管理-添加大管家");
		return $this->fetch();
		
	}
	
	// 编辑大管家
	public function editButler()
	{
		
		if(!$this->request->post())
		{
			$butler_id = input("id");
			$butler = Db::table('os_butler')->where('id', $butler_id)->find();
			$this->assign("butler",$butler);
			return $this->fetch();

		}
		else
		{
			$id = input('butlerId');
			$pwd = input('butlerPwd');
			$rePwd = input('rePwd');
			if($pwd != $rePwd)
			{
				showMsg("两次输入的密码不一致!", url("editButler",['id'=>$id]));
			}

			$arr = [
					'username' => input('butlerName'),
                    'password' => $pwd,
                    'head_img' => input('head_img'),
                    'company' => input('company'),
					'position' => input('position')
				   ];
			
			if(Db::table('os_butler')->where('id', $id)->update($arr))
			{
				showMsg("编辑管理员成功!", url("editButler",['id'=>$id]));
			}
			else
			{
				showMsg("编辑管理员失败!", url("listButler",['id'=>$id]));
			}

		}
	}
	// 删除大管家
	public function delButler()
	{
		 $butler_id = input('id');
		 
		 if(Db::name('os_butler')->delete($butler_id))
		 {
		 	showMsg("删除成功!", url("listButler"));
		 }
		 else 
		 {
		 	showMsg("删除失败!", url("listButler"));
		 }
		 		
	}
	
	// 大管家评论记录
	public function comments()
	{
		$butler_id = input('id');
		$butler = Db::table('os_butler')->where("id=$butler_id")->find();
		$avgScore = Db::table('os_comment')->where("butler_id=$butler_id")->avg('score');
		$comments = Db::table('os_comment')->where("butler_id=$butler_id")->paginate(20);
		 
		$this->assign("butlerName",$butler['username']);
		$this->assign("avgScore",$avgScore);
		$this->assign("comments",$comments);
		// 获取分页显示
		$page = $comments->render();
		$this->assign('page', $page);
		return $this->fetch();
	}
}